package com.yango.teach.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yango.serviceutil.exception.YangoException;
import com.yango.teach.entity.Subject;
import com.yango.teach.entity.vo.subject.ExcelSubjectData;
import com.yango.teach.entity.vo.subject.OneSubjectVo;
import com.yango.teach.entity.vo.subject.TwoSubjectVo;
import com.yango.teach.listener.SubjectExcelListener;
import com.yango.teach.mapper.SubjectMapper;
import com.yango.teach.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-24
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    //Excel导入课程分类-poi读取excel内容
    @Override
    public void importSubject(MultipartFile file, SubjectService subjectService) {
        try {
            //获取文件输入流
            InputStream inputStream = file.getInputStream();
            //调用方法导入
            EasyExcel.read(inputStream, ExcelSubjectData.class,
                    new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new YangoException(20001, "导入课程分类失败!");
        }
    }

    //获取分类列表（树形结构）
    @Override
    public List<OneSubjectVo> getSubjectList() {
        //最终要返回的数据列表
        ArrayList<OneSubjectVo> finalSubjectList = new ArrayList<>();
        //获取一级分类列表
        QueryWrapper<Subject> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("parent_id", 0);//根据parent_id等于零查询一级分类
        oneWrapper.orderByAsc("sort");//根据id降序排序
        List<Subject> oneList = baseMapper.selectList(oneWrapper);

        //获取二级分类数据列表
        QueryWrapper<Subject> twoWrapper = new QueryWrapper<>();
        twoWrapper.ne("parent_id", 0);//根据parent_id不等于零查询一级分类
        twoWrapper.orderByDesc("sort");//根据id和排序降序排序
        List<Subject> twoList = baseMapper.selectList(twoWrapper);

        //填充一级分类vo数据
        for (Subject oneSubject : oneList) {
            //创建一级分类vo对象
            OneSubjectVo oneSubjectVo = new OneSubjectVo();
            //将一级分类对象放进一级分类vo对象中
            BeanUtils.copyProperties(oneSubject, oneSubjectVo);
            //将一级分类vo对象放到最终返回集合中
            finalSubjectList.add(oneSubjectVo);

            //填充二级分类vo数据
            //声明集合装载二级分类数据
            ArrayList<TwoSubjectVo> twoSubjectVoList = new ArrayList<>();
            for (Subject twoSubject : twoList) {
                //判断二级分类的parent_id和一级分类id是否相同
                if (twoSubject.getParentId().equals(oneSubject.getId())) {
                    //创建二级分类vo对象
                    TwoSubjectVo twoSubjectVo = new TwoSubjectVo();
                    //将二级分类数据放到二级分类vo对象中
                    BeanUtils.copyProperties(twoSubject, twoSubjectVo);
                    //将二级分类vo对象放到二级分类数据集合中
                    twoSubjectVoList.add(twoSubjectVo);
                }

            }
            //将二级分类数据放到一级分类数据中
            oneSubjectVo.setChildren(twoSubjectVoList);
        }
        //返回数据
        return finalSubjectList;
    }


}
